package cn.tom.controller.tea;

import cn.tom.dao.*;
import cn.tom.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/tea")
public class TeaController {
    @Autowired
    UserDao userDao;
    @Autowired
    TeaDao teaDao;
    @Autowired
    ClzDao clzDao;
    @Autowired
    CourseDao courseDao;
    @Autowired
    MarkDao markDao;
    @GetMapping("find")
    public String find(Model model){
        model.addAttribute("action","update");
        return "tea/logintea";
    }
/*修改密码*/
    @RequestMapping("/update")
    public String update(String phone, String pwd , String pwd1, String pwd2, Model model) {
        System.out.println("手机号："+phone);
        System.out.println("新密码=" + pwd2);
        User user = userDao.findOneByPhone(phone);//根据手机号查询
        System.out.println(user);
        if (pwd.equals(pwd1)){      //判断新密码和旧密码是否相同
            model.addAttribute("action","update");
            System.out.println("新密码和原密码相同无法修改，请重新输入");
            model.addAttribute("pwd1","新密码和原密码相同无法修改，请重新输入");
            return "tea/logintea";
        }if (!pwd1.equals(pwd2)){   //判断两次新密码是否相同
            model.addAttribute("action","update");
            model.addAttribute("pwd1","两次新密码不同无法修改请重新输入");
            return "tea/logintea";
        }
        User user1= new User();
        user1.setPhone(phone);
        user1.setPwd(pwd2);
        user.setPwd(pwd1);
        System.out.println(user1);
        System.out.println(teaDao.update(user1));
        model.addAttribute("pwd1", "更新完毕！！！");
        return "tea/updatepwd";
    }
    /*查询该老师教的班级和课程*/
    @RequestMapping("/findcourse")
    public String findcourse(HttpSession session, Model model){
        model.addAttribute("action","findcou");
        Object who = session.getAttribute("who");
        System.out.println("who="+who);
        User user = (User) who;
        System.out.println("user="+user);
int tid=user.getUid();
        System.out.println(tid);
        List<Task> task2=teaDao.findtask(user.getUid());
        System.out.println(task2);
        model.addAttribute("task",task2);
        return "tea/logintea";
    }
    @GetMapping("/findBycouese")
    public String findBycouese(HttpSession session, String clzno, int cno,Model model){
        Object who = session.getAttribute("who");
        User user=(User) who;
        if (user==null){
            model.addAttribute("loginmsg","未登录，请登录后才能修改本人密码");
            return "main";
        }
        User user1=userDao.fiduid(user.getUid());
        model.addAttribute("user1",user1);

        List<Mark> mks=new ArrayList<>();
        Clz clz = clzDao.findByClzno(clzno);
        Course course = courseDao.findBySno(cno);
        List<User> stus=userDao.findStusByclz(clzno);

        for (User s:stus){
            Mark mk=new Mark();
            mk.setClzno(clzno);
            mk.setClzname(clz.getClzname());
            mk.setCno(cno);
            mk.setCname(course.getCname());
            mk.setSno(s.getPhone());
            mk.setSname(s.getUname());
            mk.setId(0);
            mk.setScore(0);

            Map<String,Object> map=new HashMap<>();
            map.put("sno",s.getPhone());
            map.put("cno",cno);

            Mark k= markDao.findBySnoCno(map);
            if (k!=null){
                mk.setScore(k.getScore());
                mk.setId(k.getId());
            }
            mks.add(mk);
        }
        model.addAttribute("tark",mks);
        model.addAttribute("action","findBycouese");
           return "tea/logintea";
    }
    @RequestMapping("/add")
    public String addMark(Mark mark,Model model){
        mark.setTpost(new Date());
        System.out.println(mark);
        if (mark.getId()==0){
            markDao.add(mark);
            model.addAttribute("action","findcou");
            return "forward:/tea/findcourse";
        }else {
            markDao.updatesccore(mark);
            model.addAttribute("action","findcou");
            return "forward:/tea/findcourse";

        }

    }

}
